﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Proxy
{
    public class DeleteInfoHandle
    {
        public DeleteInfoHandle()
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool DeletedSettingInfo(int Id)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var aa = Db.T_SettingsConfig.Where(s => s.Id == Id).FirstOrDefault();
                    Db.T_SettingsConfig.Remove(aa);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Delete.Error(ex.Message);
                return result;
            }
        }

        public bool DeletedPrintTask(int id)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var aa = Db.T_Nesting_LabelTask.Where(s => s.Id == id).FirstOrDefault();
                    Db.T_Nesting_LabelTask.Remove(aa);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Delete.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="Upi"></param>
        /// <returns></returns>
        public bool DeleteBoardInfoByUpi(string Upi)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var BoardInfo = Db.T_TransOnlineRecords.Where(s => s.Reference == Upi).FirstOrDefault();
                    Db.T_TransOnlineRecords.Remove(BoardInfo);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Delete.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool DeleteOnLineRecords()
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var OnlineRecordsInfo = Db.T_TransOnlineRecords.ToList();
                    Db.T_TransOnlineRecords.RemoveRange(OnlineRecordsInfo);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Delete.Error(ex.Message);
                return result;
            }
        }

        public string DeleteAllTasks()
        {
            string result = "";
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var AllTasks = Db.T_Nesting_LabelTask.ToList();
                    if (AllTasks.Count > 0)
                    {
                        Db.T_Nesting_LabelTask.RemoveRange(AllTasks);
                        Db.SaveChanges();
                        result = "Deleted successfully";
                    }
                    else
                    {
                        result = "Sorry, no current assignment.";
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Delete.Error(ex.Message);
                return result = "Failed to delete";
            }
        }

        public string DeleteBatchById(string ID)
        {
            string result = "";
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var batchInfo = Db.T_Nesting_BatchInfo.Where(s => s.BatchId == ID).FirstOrDefault();
                    var boardsInfo = Db.T_Nesting_BoardInfo.Where(s => s.BatchId == ID).ToList();
                    var partshInfo = Db.T_Nesting_PartInfo.Where(s => s.BatchId == ID).ToList();
                    var labelsInfo = Db.T_Nesting_LabelTask.Where(s => s.BatchId == ID).ToList();
                    var pnxsInfo = Db.T_Nesting_PnxInfo.Where(s => s.Run == ID).ToList();
                    Db.T_Nesting_BatchInfo.Remove(batchInfo);
                    Db.T_Nesting_BoardInfo.RemoveRange(boardsInfo);
                    Db.T_Nesting_PartInfo.RemoveRange(partshInfo);
                    if (labelsInfo.Count != 0)
                    {
                        Db.T_Nesting_LabelTask.RemoveRange(labelsInfo);
                    }
                    Db.T_Nesting_PnxInfo.RemoveRange(pnxsInfo);
                    Db.SaveChanges();
                    result = "Deleted successfully";
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Delete.Error(ex.Message);
                return result = "Failed to delete";
            }
        }

        public string DeleteMprById(int BoardInfoId)
        {
            string result = "";
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    var boardInfo = Db.T_Nesting_BoardInfo.Where(s => s.Id == BoardInfoId).FirstOrDefault();
                    Db.T_Nesting_BoardInfo.Remove(boardInfo);
                    Db.SaveChanges();
                    List<T_Nesting_LabelTask> labelTasks = Db.T_Nesting_LabelTask.Where(s => s.BoardInfoId == BoardInfoId).ToList();
                    Db.T_Nesting_LabelTask.RemoveRange(labelTasks);
                    Db.SaveChanges();
                    result = "Deleted successfully";
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Delete.Error(ex.Message);
                return result = "Failed to delete";
            }
        }
    }
}